/**
 * Class: Timeframe
 *
 * Purpose: 
 *
 * Created: Feb 15, 2010 @ 10:48:03 AM
 * @author Ken Scott
 */
package org.openfantasia.ps.modifier.behaviors;

import org.openfantasia.ps.particle.Particle;
import org.openfantasia.util.math.DRange;

public class Timeframe extends DRange implements Condition {

	public Timeframe(Double startTime, Double endTime) {
		this.min = startTime != null ? Math.max(0, startTime) : 0;
		this.max = endTime != null ? Math.min(Double.MAX_VALUE, endTime) : Double.MAX_VALUE;
	}


	public Timeframe(Timeframe that) {
		copy(that);
	}


	public Timeframe copy() {
		return new Timeframe(this);
	}


	public void copy(Timeframe that) {
		this.min = that.min;
		this.max = that.max;
	}


	public Double getStartTime() {
		return min;
	}


	public void setStartTime(Double startTime) {
		this.min = startTime;
	}


	public Double getEndTime() {
		return max;
	}


	public void setEndTime(Double endTime) {
		this.max = endTime;
	}


	public boolean isActive(Particle particle, Object param) {
		boolean result = false;

		if (param instanceof Double) {
			result = isWithin((Double) param);
		}

		return result;
	}


	public double getInternalTime(double time) {
		double result = time - min;

		if (time <= min) {
			result = 0;				// before start-time
		}
		else if (time > max) {
			result = max;
		}

		return result;
	}


	public double getDuration() {
		return (max - min);
	}


	public double getInternalFraction(double time) {
		return getInternalTime(time) / getDuration();
	}


	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Timeframe)) {
			return false;
		}

		Timeframe timeframe = (Timeframe) o;

		if (max != null ? !max.equals(timeframe.max) : timeframe.max != null) {
			return false;
		}
		if (min != null ? !min.equals(timeframe.min) : timeframe.min != null) {
			return false;
		}

		return true;
	}


	@Override
	public int hashCode() {
		int result = min != null ? min.hashCode() : 0;
		result = 31 * result + (max != null ? max.hashCode() : 0);
		return result;
	}


	@Override
	public String toString() {
		return "Timeframe{" +
				"min=" + min +
				", max=" + max +
				'}';
	}
}
